using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;

public class SaveAndLoad : MonoBehaviour
{
	public List<Transform> objsSave;

	private string folder;

	private void Awake()
	{
		//if (Application.platform == RuntimePlatform.Android)
		//{
		//	AndroidJavaClass androidJavaClass = new AndroidJavaClass("android.os.Environment");
		//	using (AndroidJavaObject androidJavaObject = androidJavaClass.CallStatic<AndroidJavaObject>("getExternalStorageDirectory", new object[0]))
		//	{
		//		string text = androidJavaObject.Call<string>("getPath", new object[0]);
		//		folder = text + "/SimpleSandbox/Saves/";
		//		return;
		//	}
		//}
		//folder = "Assets/SavePc/";
		//if (!Directory.Exists(folder))
		//{
		//	Directory.CreateDirectory(folder);
		//}
	}

	private void LoadLevelOnStart()
	{
		string @string = PlayerPrefs.GetString("Level");
		if (@string != string.Empty)
		{
			Load(@string);
		}
	}

	private void Update()
	{
		if (Input.GetKeyDown(KeyCode.L))
		{
			Load("Test");
		}
		if (Input.GetKeyDown(KeyCode.P))
		{
			Save("Test");
		}
	}

	public void Save(string fileName)
	{
		Debug.Log("Save");
		GameObject[] array = GameObject.FindGameObjectsWithTag("Item");
		GameObject[] array2 = array;
		foreach (GameObject gameObject in array2)
		{
			if (gameObject.transform.parent == null)
			{
				objsSave.Add(gameObject.transform);
			}
		}
		XmlDocument xmlDocument = new XmlDocument();
		XmlNode xmlNode = xmlDocument.CreateElement(Application.loadedLevelName);
		xmlDocument.AppendChild(xmlNode);
		for (int j = 0; j < objsSave.Count && j < 250; j++)
		{
			XmlElement xmlElement = xmlDocument.CreateElement(objsSave[j].name);
			Rigidbody component = objsSave[j].GetComponent<Rigidbody>();
			xmlElement.SetAttributeNode(attribute("Px", xmlDocument, objsSave[j].position.x));
			xmlElement.SetAttributeNode(attribute("Py", xmlDocument, objsSave[j].position.y));
			xmlElement.SetAttributeNode(attribute("Pz", xmlDocument, objsSave[j].position.z));
			xmlElement.SetAttributeNode(attribute("Rx", xmlDocument, objsSave[j].eulerAngles.x));
			xmlElement.SetAttributeNode(attribute("Ry", xmlDocument, objsSave[j].eulerAngles.y));
			xmlElement.SetAttributeNode(attribute("Rz", xmlDocument, objsSave[j].eulerAngles.z));
			xmlElement.SetAttributeNode(attribute("Vx", xmlDocument, component.velocity.x));
			xmlElement.SetAttributeNode(attribute("Vy", xmlDocument, component.velocity.y));
			xmlElement.SetAttributeNode(attribute("Vz", xmlDocument, component.velocity.z));
			xmlElement.SetAttributeNode(attribute("Mass", xmlDocument, component.mass));
			xmlElement.SetAttributeNode(attribute("Gravity", xmlDocument, component.useGravity));
			xmlElement.SetAttributeNode(attribute("Kinematic", xmlDocument, component.isKinematic));
			Transform[] componentsInChildren = objsSave[j].GetComponentsInChildren<Transform>();
			for (int k = 1; k < componentsInChildren.Length; k++)
			{
				if (!(componentsInChildren[k].tag != "WeldObject") || !(componentsInChildren[k].tag != "NotWeldObject") || !(componentsInChildren[k].tag != "Thruster") || !(componentsInChildren[k].tag != "HoverBall"))
				{
					XmlElement xmlElement2 = xmlDocument.CreateElement(componentsInChildren[k].name);
					xmlElement2.SetAttributeNode(attribute("Px", xmlDocument, componentsInChildren[k].localPosition.x));
					xmlElement2.SetAttributeNode(attribute("Py", xmlDocument, componentsInChildren[k].localPosition.y));
					xmlElement2.SetAttributeNode(attribute("Pz", xmlDocument, componentsInChildren[k].localPosition.z));
					xmlElement2.SetAttributeNode(attribute("Rx", xmlDocument, componentsInChildren[k].localEulerAngles.x));
					xmlElement2.SetAttributeNode(attribute("Ry", xmlDocument, componentsInChildren[k].localEulerAngles.y));
					xmlElement2.SetAttributeNode(attribute("Rz", xmlDocument, componentsInChildren[k].localEulerAngles.z));
					xmlElement.AppendChild(xmlElement2);
				}
			}
			xmlNode.AppendChild(xmlElement);
		}
		objsSave.Clear();
		xmlDocument.Save(folder + fileName + ".xml");
	}

	public void Load(string fileName)
	{
		fileName += ".xml";
		if (!File.Exists(folder + fileName))
		{
			Debug.Log("Файла нет");
			return;
		}
		XmlTextReader xmlTextReader = new XmlTextReader(folder + fileName);
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(xmlTextReader);
		XmlNode documentElement = xmlDocument.DocumentElement;
		foreach (XmlNode childNode in documentElement.ChildNodes)
		{
			GameObject gameObject = Resources.Load<GameObject>(childNode.Name);
			if (gameObject == null)
			{
				continue;
			}
			Rigidbody component = gameObject.GetComponent<Rigidbody>();
			Vector3 position = new Vector3(GetFloat(childNode, "Px"), GetFloat(childNode, "Py"), GetFloat(childNode, "Pz"));
			Vector3 euler = new Vector3(GetFloat(childNode, "Rx"), GetFloat(childNode, "Ry"), GetFloat(childNode, "Rz"));
			Vector3 velocity = new Vector3(GetFloat(childNode, "Vx"), GetFloat(childNode, "Vy"), GetFloat(childNode, "Vz"));
			float @float = GetFloat(childNode, "Mass");
			bool @bool = GetBool(childNode, "Gravity");
			bool bool2 = GetBool(childNode, "Kinematic");
			Rigidbody rigidbody = null;
			if (PhotonNetwork.room != null && !PhotonNetwork.offlineMode)
			{
				GameObject gameObject2 = PhotonNetwork.InstantiateSceneObject(childNode.Name, position, Quaternion.identity, 0, null);
				rigidbody = gameObject2.GetComponent<Rigidbody>();
			}
			else
			{
				rigidbody = Object.Instantiate(component, position, Quaternion.identity) as Rigidbody;
			}
			rigidbody.isKinematic = true;
			rigidbody.position = position;
			rigidbody.rotation = Quaternion.Euler(euler);
			rigidbody.GetComponent<ItemFunctions>().m_ObjectIsKinematic = bool2;
			rigidbody.isKinematic = bool2;
			rigidbody.useGravity = @bool;
			rigidbody.velocity = velocity;
			rigidbody.name = childNode.Name;
			foreach (XmlNode childNode2 in childNode.ChildNodes)
			{
				Vector3 localPosition = new Vector3(GetFloat(childNode2, "Px"), GetFloat(childNode2, "Py"), GetFloat(childNode2, "Pz"));
				Vector3 euler2 = new Vector3(GetFloat(childNode2, "Rx"), GetFloat(childNode2, "Ry"), GetFloat(childNode2, "Rz"));
				GameObject gameObject3 = Resources.Load<GameObject>(childNode2.Name);
				if (!(gameObject3 == null))
				{
					GameObject gameObject4 = Object.Instantiate(gameObject3, Vector3.zero, Quaternion.identity) as GameObject;
					Rigidbody component2 = gameObject4.GetComponent<Rigidbody>();
					if (component2 != null && gameObject4.tag != "HoverBall")
					{
						component2.isKinematic = true;
						Object.Destroy(component2);
					}
					gameObject4.transform.SetParent(rigidbody.transform);
					gameObject4.transform.localPosition = localPosition;
					gameObject4.transform.localRotation = Quaternion.Euler(euler2);
					gameObject4.name = childNode2.Name;
				}
			}
		}
		xmlTextReader.Close();
	}

	private XmlAttribute attribute(string name, XmlDocument doc, float value)
	{
		XmlAttribute xmlAttribute = doc.CreateAttribute(name);
		xmlAttribute.Value = value.ToString();
		return xmlAttribute;
	}

	private XmlAttribute attribute(string name, XmlDocument doc, int value)
	{
		XmlAttribute xmlAttribute = doc.CreateAttribute(name);
		xmlAttribute.Value = value.ToString();
		return xmlAttribute;
	}

	private XmlAttribute attribute(string name, XmlDocument doc, bool value)
	{
		XmlAttribute xmlAttribute = doc.CreateAttribute(name);
		xmlAttribute.Value = value.ToString();
		return xmlAttribute;
	}

	private XmlAttribute attribute(string name, XmlDocument doc, string value)
	{
		XmlAttribute xmlAttribute = doc.CreateAttribute(name);
		xmlAttribute.Value = value;
		return xmlAttribute;
	}

	private float GetFloat(XmlNode node, string name)
	{
		try
		{
			return float.Parse(node.Attributes[name].Value);
		}
		catch
		{
			return 0f;
		}
	}

	private int GetInt(XmlNode node, string name)
	{
		try
		{
			return int.Parse(node.Attributes[name].Value);
		}
		catch
		{
			return 0;
		}
	}

	private bool GetBool(XmlNode node, string name)
	{
		try
		{
			return bool.Parse(node.Attributes[name].Value);
		}
		catch
		{
			return false;
		}
	}
}
